"use strict";

/**
 * @ngdoc function
 * @name jgappApp.controller:ContractCgcontractCtrl
 * @description
 * # ContractCgcontractCtrl
 * Controller of the jgappApp
 */
angular
  .module("jgappApp")
  .controller("ContractCgcontractCtrl", function (
    $scope,
    $rootScope,
    $location,
    openApi,
    Util,
    $routeParams,
    FormSubmit,
    localStorageService
  ) {
    var id = ($scope.process_id = $routeParams.id);
    // 税率
    $scope.rateList = [{
      key: "无",
      value: "无"
    }, {
      key: "1%",
      value: "1%"
    }, {
      key: "2%",
      value: "2%"
    }, {
      key: "3%",
      value: "3%"
    }, {
      key: "5%",
      value: "5%"
    }, {
      key: "6%",
      value: "6%"
    }, {
      key: "9%",
      value: "9%"
    }, {
      key: "10%",
      value: "10%"
    }, {
      key: "11%",
      value: "11%"
    }, {
      key: "13%",
      value: "13%"
    }, {
      key: "16%",
      value: "16%"
    }, {
      key: "17%",
      value: "17%"
    }, {
      key: "免税",
      value: "免税"
    }, {
      key: "其他",
      value: "其他"
    }];
    // 发票类型
    $scope.invoiceTypeList = [
      {
        key: "增值税普通发票",
        value: "增值税普通发票"
      },
      {
        key: "增值税专用发票",
        value: "增值税专用发票"
      },
      {
        key: "普通商业发票",
        value: "普通商业发票"
      },
      {
        key: "其它",
        value: "其它"
      }
    ];
    // 选择款项类别
    $scope.costTypeList = [
      {
        key: "工程款",
        value: 1
      },
      {
        key: "垫付款",
        value: 2
      },
      {
        key: "预借款",
        value: 8
      },
      {
        key: "清账款",
        value: 9
      },
      {
        key: "其他收入",
        value: 13
      }
    ];
    $scope.vatlist = [{ key: "含税", value: 1 }, { key: "不含税", value: 0 }];
    // 选择材料类型
    $scope.getmaterialTypes = function () {
      var param = {};
      param.type = "材料类别";
      Util.getKeyValue(param, function (data) {
        $scope.materialTypeList = data.data;
      });
    };
    $scope.chooseProject = function () {
      // 将列表的数据序列化存入缓存以便回来时候使用
      Util.savaParamForReview($scope.param, id);
      $location.path("project/chooseproject");
    };

    $scope.chooseSupplier = function () {
      var params = $scope.param;
      var projectParams = {
        formKey: "Xm_SupplierStore",
        fieldName: "supplierNo"
      };

      // 清掉关联的编号
      Util.savaParamForReview(params, id);
      localStorageService.set("projectParams", projectParams);
      $location.path("/project/choosedata");
    };

    $scope.showPicker = function () {
      $scope.showCerPicker = true;
      $rootScope.$emit("showmasker");
    };
    $scope.deletedItem = function (index) {
      $scope.param.details.splice(index, 1);
      $scope.curindex = -1;
    };

    $scope.addItem = function () {
      $scope.picker = $scope.picker || {};
      var check = false;
      check = checkPicker($scope.picker);
      if (!check) {
        return;
      }
      $scope.param.details.push($scope.picker);
      // 隐藏表单
      closePicker();
    };
    var closePicker = function () {
      $scope.picker = {};
      $rootScope.$emit("closemasker");
      $scope.showCerPicker = false;
    };
    $scope.$on("closemasker", function () {
      closePicker();
    });
    $scope.closePicker = function () {
      closePicker();
    };
    $scope.swipe = function (index, type) {
      if (type) {
        $scope.payindex = index;
      } else {
        $scope.curindex = index;
      }
    };
    $scope.resetlist = function (type) {
      if (type) {
        $scope.payindex = -1;
      } else {
        $scope.curindex = -1;
      }
    };
    // 验证是否输入完整
    var checkPicker = function (data) {
      if (!data.name) {
        Util.Toast("请填写物品名称");
        return;
      }
      return true;
    };
    $scope.submit = function (operationId) {
      // 底部类型end
      var options = {};
      // 流程id
      options.id = id;
      // 根据operationId判断是草稿还是提交
      options.operationId = operationId;
      options.urlParam = openApi.contractpurchase;
      options.param = angular.copy($scope.param, {});
      options.param.purchases = JSON.stringify(options.param.details);
      if (options.param.relevances) {
        options.param.relevanceIds = options.param.relevances.map(function (item) {
          return item.targetId;
        });
      }
      if (options.param.invoiceRevs) {
        options.param.invoiceRevIds = options.param.invoiceRevs.map(function (item) {
          return item.invoiceId;
        });
      }
      delete options.param.invoiceRevs;
      delete options.param.relevances;
      delete options.param.details;
      FormSubmit.submit(options, $scope);
    };
    $scope.validate = function () {
      if (!$scope.param.projectName) {
        Util.Toast("请选择项目");
        return;
      }
      if (!$scope.param.contractName) {
        Util.Toast("请填写合同名称");
        return;
      }
      if (!$scope.param.materialType) {
        Util.Toast("请选择材料类别");
        return;
      }
      if (!$scope.param.partA) {
        Util.Toast("请填写甲方");
        return;
      }
      if (!$scope.param.partB) {
        Util.Toast("请填写乙方");
        return;
      }
      if (!$scope.param.invoiceType) {
        Util.Toast("请选择发票类型");
        return;
      }
      if (!$scope.param.taxRate) {
        Util.Toast("请选择税率");
        return;
      }
      if (!$scope.param.taxInclusive && $scope.param.taxInclusive !== 0) {
        Util.Toast("请选择是否含税");
        return;
      }
      if (!$scope.param.contractAmount && $scope.param.contractAmount !== 0) {
        Util.Toast("请输入合同金额");
        return;
      }

      if (!$scope.validation.passvalidation) {
        Util.Toast($scope.validation.tip);
        return;
      }
      return true;
    };
    var init = function () {
      // 获取回显的详细信息
      Util.getDetailData(id, function (data) {
        $scope.param = data;
        var data = localStorageService.get("projectinfo");
        localStorageService.remove("projectinfo");
        // 获取项目关联数据
        var projectreldata = localStorageService.get("projectreldata");
        localStorageService.remove("projectreldata");
        $scope.param = Object.assign({}, $scope.param, data, projectreldata);
        $scope.param.details = $scope.param.details || []
      });
      $scope.getmaterialTypes();
    };
    // 初始化
    init();
  });
